package pkg.hicph.org;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

public class NewGameMenu extends Activity implements OnClickListener {

	// Preference file
    public static final String PREF_FILE = "pf_file";
    private SharedPreferences preferences;
	
	Button startGameButton, creditsButton;
	TextView infoUser;
	Double latDoubleValue,lonDoubleValue;
	
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(
        WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.newgamemenu);
        
        registerViews();
        setupOnClickListeners();
        
        startGameButton.setText("Please wait! Loading location...");
        startGameButton.setEnabled(false);
        
        // Acquire a reference to the system Location Manager
        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
		
        // Define a listener that responds to location updates
        LocationListener locationListener = new LocationListener() {
            
            public void onLocationChanged(Location location) {

                latDoubleValue = location.getLatitude();
                lonDoubleValue = location.getLongitude();
                
                
                    // if you created the game
                    // center the map on your first position and update 
                    // the center values on the database

                    if(latDoubleValue!=0 && lonDoubleValue!=0) {
                        // if we already have a clear position
                        // set the center position of the game

                        latDoubleValue = latDoubleValue * 1E6;
                        lonDoubleValue = lonDoubleValue * 1E6;
                        
                        Log.d("CENTER LAT", latDoubleValue+"");
                        Log.d("CENTER LON", lonDoubleValue+"");

                        preferences  = getSharedPreferences(PREF_FILE, MODE_PRIVATE);
                        preferences.edit().putInt("latDoubleValuePref", latDoubleValue.intValue()).commit();
                        preferences.edit().putInt("lonDoubleValuePref", lonDoubleValue.intValue()).commit();
                        
                        startGameButton.setText("Ready! Click to start!");
                        startGameButton.setEnabled(true);
//                        infoUser.setText("Congratulations for waiting!");
                          
                            
                        }
  
                    }
            
				@Override
				public void onProviderDisabled(String arg0) {
				    // do nothing
					
				}
	
				@Override
				public void onProviderEnabled(String arg0) {
					// do nothing
					
				}
	
				@Override
				public void onStatusChanged(String provider, int status,
						Bundle extras) {
				    // do nothing
					
				}
           };
           
        // Register the listener with the Location Manager to receive location
       // updates from the GPS and the cell towers
       locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 2, locationListener);
       //locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 5000, 2, locationListener);
    }

    private void registerViews() {
		
    	startGameButton 	= (Button)this.findViewById(R.id.new_game_menu_start_button);
//    	infoUser 			= (TextView)this.findViewById(R.id.new_game_menu_info);
    	
	}

	public void setupOnClickListeners() {
        
		startGameButton.setOnClickListener(this);

    }
    
    public void onClick(View V) {
    	
    	switch(V.getId()) {
    	
    	case R.id.new_game_menu_start_button:
    		
    		Utils.gamePhase = Utils.NEWLY_CREATED;
    		
    		Intent mainGameMapIntent = new Intent(this, MainGameMap.class);
    		startActivity(mainGameMapIntent);
    		
    		break;

    	}

    }
}